package com.im.partchat.ui.activity;

import android.app.AlertDialog;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.AppCompatButton;
import android.support.v7.widget.AppCompatEditText;
import android.text.TextUtils;
import android.view.View;
import android.view.Window;
import android.widget.TextView;
import android.widget.Toast;

import com.im.partchat.App;
import com.im.partchat.R;
import com.im.partchat.base.BaseActivity;
import com.im.partchat.bean.UserBean;
import com.im.partchat.mvp.persenter.LoginPresenter;
import com.im.partchat.utils.MD5Utils;
import com.im.partchat.utils.Navigation;

import butterknife.BindView;
import butterknife.OnClick;
import io.rong.imlib.RongIMClient;

public class LoginActivity extends BaseActivity {

    @BindView(R.id.ed_userName)
    AppCompatEditText edUserName;
    @BindView(R.id.ed_password)
    AppCompatEditText edPassword;
    @BindView(R.id.btn_login)
    AppCompatButton btnLogin;
    private LoginPresenter loginPresenter;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_login;
    }

    @Override
    protected void initData() {
        loginPresenter = new LoginPresenter(this,this);
    }

    @Override
    protected void initView(@Nullable Bundle savedInstanceState) {
        super.initView(savedInstanceState);
        if (getIntent().getBooleanExtra("kickedByOtherClient", false)) {
            final AlertDialog dlg = new AlertDialog.Builder(LoginActivity.this).create();
            dlg.show();
            Window window = dlg.getWindow();
            window.setContentView(R.layout.other_devices);
            TextView text = (TextView) window.findViewById(R.id.ok);
            text.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    dlg.cancel();
                }
            });
        }
    }

    @Override
    public void onLoadDataSuccess(Object o, String flag) {
        UserBean userBean = App.getUserBean();
        if (userBean == null || TextUtils.isEmpty(userBean.getToken())) {
            showToast("获取token失败");
            return;
        }
        showLoading("正在连接聊天服务器");
        connect(userBean.getToken(), new RongIMClient.ConnectCallback() {
            @Override
            public void onTokenIncorrect() {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        Toast.makeText(LoginActivity.this, "token失效，请重新登陆", Toast.LENGTH_SHORT).show();
                    }
                });
                hideLoading();

            }

            @Override
            public void onSuccess(String userId) {
                hideLoading();
                Navigation.navigateMain();
                finish();
            }

            @Override
            public void onError(RongIMClient.ErrorCode errorCode) {
                Toast.makeText(LoginActivity.this, "聊天服务器异常:" + errorCode, Toast.LENGTH_SHORT).show();
                hideLoading();
            }
        });

    }


    @OnClick(R.id.btn_login)
    public void onViewClicked() {
        String userName = edUserName.getText().toString();
        String password = edPassword.getText().toString();
        if (TextUtils.isEmpty(userName)) {

            showToast("手机号不能为空");
            return;
        }
        if (TextUtils.isEmpty(password)) {
            showToast("密码不能为空");
            return;
        }

        loginPresenter.login(userName, MD5Utils.getPwd(password));
    }

    @Override
    protected boolean hasToolBar() {
        return false;
    }
}
